package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.DateUtils;
import com.central.app.service.HandlerService;
import com.central.apps.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 处理任务管理业务逻辑
 *
 * @author lidachao
 * @date 2023/2/17
 */
@Component
@Slf4j
public class ScheduleTaskHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private TaskService taskService;

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if ("gq_scheduleplantask".equals(objName)) {
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");

            boolean bool = DateUtils.isWeekEnd(startTime);
            if (bool) {
                return "开始日期不能为周六日!";
            }
            bool = DateUtils.isWeekEnd(endTime);
            if (bool) {
                return "结束日期不能为周六日!";
            }
        }
        return null;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("gq_scheduleplantask".equals(objName)) {
            Integer id = MapUtils.getInteger(data, "id");
            Integer proId = MapUtils.getInteger(data, "proid");
            Integer parentId = MapUtils.getInteger(data, "parentId");
            Integer manager = MapUtils.getInteger(data, "manager");
            Integer level = 1;
            Integer parentLevel = 0;

            // 更新任务对应层级
            if (parentId != 0) {
                List<Map<String, Object>> parentTaskList = handlerService.findBySql("select level from gq_scheduleplantask where proid =" + proId + " and id = " + parentId);
                parentLevel = MapUtils.getInteger(parentTaskList.get(0), "level");
                level = parentLevel + 1;

                handlerService.updateBySql("update gq_scheduleplantask set level = " + level + ", parentLevel = " + parentLevel + " where id = " + id);
            } else {
                if (manager != 0) {
                    List<Map<String, Object>> list = handlerService.findBySql("select id from sys_user where field10 = " + manager);
                    if (CollectionUtils.isNotEmpty(list)) {
                        Integer userId = MapUtils.getInteger(list.get(0), "id");
                        handlerService.updateBySql("update gq_proaccount set createuser = " + userId + " where createuser != " + userId + " and id = " + proId);
                    }
                }
            }

            // 重新计算工期
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");
            long duration = DateUtils.daysBetweenExpWeekEnd(startTime, endTime);
            handlerService.updateBySql("update gq_scheduleplantask set duration = " + duration + " where id = " + id);

            // 重新整理记录id
            taskService.updateRecordId(proId);
            // 重新整理父子关系
            taskService.updParentId(proId);
            // 更新前置任务信息
            taskService.updPredecessors(proId);
        }
    }
}
